﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class CS_BJHXC_Edit : BasePage
    {
        private string CPXBH
        {
            get { return this.GetUrlParam("cpxbh") == null ? "" : this.GetUrlParam("cpxbh").ToString(); }
        }
        private string HXCBH
        {
            get { return this.GetUrlParam("hxcbh") == null ? "" : this.GetUrlParam("hxcbh").ToString(); }
        }
        private string BJBH
        {
            get { return this.GetUrlParam("bh") == null ? "" : this.GetUrlParam("bh").ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCPX(this.ddlCPX, "");
                string cpxbh = this.ddlCPX.Items.Count > 0 ? this.ddlCPX.Items[0].Value : "";
                BindCPHXC(this.ddlCPHXC, cpxbh, "");
                if (!string.IsNullOrEmpty(CPXBH) && !string.IsNullOrEmpty(BJBH) && !string.IsNullOrEmpty(HXCBH))
                    BindData();
            }
        }

        private void BindData()
        {
            liTitle.Text = "修改";
            txtBH.Enabled = false;
            using (CS_BJHXCBLL cBLL = new CS_BJHXCBLL())
            {
                CS_BJHXC item = cBLL.GetEntity(t => t.BH == BJBH && t.CPXBH == CPXBH && t.CPMCBH == HXCBH);
                if (this.ddlCPX.Items.FindByValue(item.CPXBH) != null)
                {
                    this.ddlCPX.Items.FindByValue(CPXBH).Selected = true;
                    BindCPHXC(this.ddlCPHXC, this.ddlCPX.SelectedValue, "");
                }
                this.ddlCPX.Enabled = false;
                this.ddlCPHXC.Enabled = false;
                txtMC.Text = item.MC;
                txtBH.Text = item.BH;
            }
        }

        protected void ddlCPX_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCPHXC(this.ddlCPHXC, this.ddlCPX.SelectedValue, "");
        }


        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            int result = 0;
            using (CS_BJHXCBLL cBLL = new CS_BJHXCBLL())
            {
                CS_BJHXC item = new CS_BJHXC();
                if (!string.IsNullOrEmpty(CPXBH) && !string.IsNullOrEmpty(HXCBH) && !string.IsNullOrEmpty(BJBH))
                    item = cBLL.GetEntity(t => t.BH == BJBH && t.CPXBH == CPXBH && t.CPMCBH == HXCBH);
                item.CPXBH = ddlCPX.SelectedValue;
                item.CPMCBH = ddlCPHXC.SelectedValue;
                item.BH = txtBH.Text.Trim();
                item.MC = txtMC.Text.Trim();
                if (!string.IsNullOrEmpty(CPXBH) && !string.IsNullOrEmpty(HXCBH) && !string.IsNullOrEmpty(BJBH))
                    result = cBLL.SaveEntity(item, t => t.BH != item.BH && t.MC == item.MC && t.CPXBH == item.CPXBH && t.CPMCBH == HXCBH);
                else
                    result = cBLL.SaveEntity(item, t => t.BH == item.BH && t.MC == item.MC && t.CPXBH == item.CPXBH && t.CPMCBH == HXCBH);
            }
            if (result == 1)
                labResult.Text = "保存成功";
            else if (result == -1)
                labResult.Text = "已存在此部件，请重新输入";
            else
                labResult.Text = "保存失败";
        }
    }
}